package com.zqj.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zqj.project.common.annotation.PreventDuplicateSubmit;
import com.zqj.project.common.entity.R;
import com.zqj.project.common.request.PageParams;
import com.zqj.project.converter.OrderDetailConverter;
import com.zqj.project.dto.OrderDetailDTO;
import com.zqj.project.service.OrderDetailService;
import com.zqj.project.vo.OrderDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Description: OrderDetailController
 * @Author: 22001010704 Zhang Qijian
 * @CreateTime: 2024/12/8 19:38
 * @Version: 1.0
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/orderDetail")
@Tag(name = "OrderDetail API", description = "订单明细管理接口，支持增删改查以及分页功能")
@RequiredArgsConstructor
public class OrderDetailController {

    // 自动生成依赖注入的构造函数
    private final OrderDetailService orderDetailService;

    // 分页查询订单明细列表
    @Operation(summary = "分页查询订单明细列表", description = "根据分页参数查询订单明细信息")
    @PostMapping("/page")
    public R<IPage<OrderDetailVO>> queryOrderDetailPage(@RequestBody @Validated @NotNull @Parameter(description = "分页参数及筛选条件") PageParams<OrderDetailDTO> pageParams) {
        OrderDetailDTO orderDetailDTO = pageParams.getModel();
        return R.success(OrderDetailConverter.toOrderDetailVoPage(this.orderDetailService.page(OrderDetailConverter.toOrderDetailModel(orderDetailDTO), new Page<>(pageParams.getCurrent(), pageParams.getSize()))));
    }

    // 查询订单明细详情
    @Operation(summary = "查询订单明细详情", description = "根据订单明细ID查询具体订单明细的详细信息")
    @GetMapping("/info/{id}")
    public R<OrderDetailVO> info(@PathVariable @Parameter(description = "订单明细ID", required = true) Long id) {
        return R.success(OrderDetailConverter.toOrderDetailVo(this.orderDetailService.info(id)));
    }

    // 新增订单明细
    @Operation(summary = "新增订单明细", description = "新增一条新的订单明细记录")
    @PostMapping("/add")
    @PreventDuplicateSubmit
    public R<String> add(@RequestBody @Validated @Parameter(description = "要新增的订单明细信息") OrderDetailDTO orderDetailDto) {
        return Boolean.TRUE.equals(this.orderDetailService.add(OrderDetailConverter.toOrderDetailModel(orderDetailDto))) ? R.success("订单明细新增成功") : R.fail("订单明细新增失败");
    }

    // 修改订单明细
    @Operation(summary = "修改订单明细", description = "根据订单明细ID更新订单明细信息")
    @PutMapping("/edit")
    @PreventDuplicateSubmit
    public R<String> edit(@RequestBody @Validated @Parameter(description = "要修改的订单明细信息，需包含ID") OrderDetailDTO orderDetailDto) {
        if (Objects.isNull(orderDetailDto.getId())) {
            return R.fail("订单明细ID不能为空");
        }
        return Boolean.TRUE.equals(this.orderDetailService.edit(OrderDetailConverter.toOrderDetailModel(orderDetailDto))) ? R.success("订单明细修改成功") : R.fail("订单明细修改失败");
    }

    // 删除订单明细
    @Operation(summary = "删除订单明细", description = "根据订单明细ID删除订单明细信息")
    @DeleteMapping("/delete/{id}")
    @PreventDuplicateSubmit
    public R<String> delete(@PathVariable @Parameter(description = "订单明细ID", required = true) Long id) {
        return Boolean.TRUE.equals(this.orderDetailService.delete(id)) ? R.success("订单明细删除成功") : R.fail("订单明细删除失败");
    }

}